package edu.pw.p2pclient.networking.webservice;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class ActiveRequestsResponse extends Response {

	List<ActiveRequest> mRequests = null;

	public ActiveRequestsResponse(int exitCode, String message,
			ActiveRequest[] requests) {
		super(exitCode, message);
		mRequests = new ArrayList<ActiveRequest>(requests.length);
		for (ActiveRequest req : requests)
			mRequests.add(req);
	}

	public List<ActiveRequest> getRequests() {
		return mRequests;
	}

	public final static JsonDeserializer<ActiveRequestsResponse> deserializer = new JsonDeserializer<ActiveRequestsResponse>() {

		@Override
		public ActiveRequestsResponse deserialize(JsonElement json,
				Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {

			int exitCode = json.getAsJsonObject().get("exitCode").getAsInt();
			String message = json.getAsJsonObject().get("message")
					.getAsString();
			ActiveRequest[] requests = context.deserialize(json
					.getAsJsonObject().get("result"), ActiveRequest[].class);

			return new ActiveRequestsResponse(exitCode, message, requests);
		}
	};

}
